/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.thinkgem.jeesite.common.utils.EnumProperty;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.bus.dao.EntDao;
import com.thinkgem.jeesite.modules.bus.dao.EntHistoryDao;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.bus.entity.Audit;
import com.thinkgem.jeesite.modules.bus.entity.Ent;
import com.thinkgem.jeesite.modules.bus.entity.EntHistory;
import com.thinkgem.jeesite.modules.bus.dao.AuditDao;

/**
 * 养殖企业信息Service
 * @author mengying
 * @version 2019-05-30
 */
@Service
@Transactional(readOnly = true)
public class AuditService extends CrudService<AuditDao, Audit> {
	@Autowired
	public  AuditDao auditDao;
	@Autowired
	public EntDao entDao;
	@Autowired
	public EntHistoryDao entHistoryDao;

	public Audit get(String id) {
		return super.get(id);
	}

	public List<Audit> findList(Audit audit) {
		return super.findList(audit);
	}

	public Page<Audit> findPage(Page<Audit> page, Audit audit) {
		return super.findPage(page, audit);
	}

	@Transactional(readOnly = false)
	public void save(Audit audit) {
		super.save(audit);
	}

	@Transactional(readOnly = false)
	public void delete(Audit audit) {
		super.delete(audit);
	}

	public Audit chenckEntInfo(String entId) {
		return auditDao.chenckEntInfo(entId);
	}

	/**
	 *企业待审核列表
	 * @author mengying
	 * @param page
	 * @return String
	 */
	public Page<Audit> uncheckedList(Page<Audit> page,Audit audit) {
		User user= UserUtils.getUser();
		String areaCode=user.getOffice().getCode();
		audit.setAreaCode(areaCode);
		audit.setPage(page);
		page.setList(auditDao.uncheckedList(audit));
		return page;
	}
	/**
	 * 
	* @Title: uncheckedList 
	* @author: ZYJ
	* @date: 2019年6月22日 上午10:50:06 
	* @Description: 养殖场备案待审核列表
	* @param:  page
	* @param:  audit
	* @param:  auditStatus
	* @param:  statusFlag
	* @return: Page<Audit>
	* @throws
	 */
	public Page<Audit> uncheckedEntList(Page<Audit> page,Audit audit) {
		String areaCode=UserUtils.getUser().getOffice().getCode();
		audit.setAreaCode(areaCode);
		audit.setPage(page);
		page.setList(auditDao.uncheckedEntList(audit));
		return page;
	}

	/**
	 *企业已审核列表
	 * @author mengying
	 * @param page
	 * @return String
	 */
	public Page<Audit> checkedList(Page<Audit> page,Audit audit) {
		User user= UserUtils.getUser();
		String areaCode=user.getOffice().getCode();
		audit.setAreaCode(areaCode);
		audit.setPage(page);
		page.setList(auditDao.checkedList(audit));
		return page;
	}
	
	/**
	 *改变企业审核状态
	 * @author mengying
	 * @param entId
	 * @param updateFlag
	 * @return String
	 */
	@Transactional(readOnly = false)
	public boolean changeAuditStatus(String entId,String updateFlag,String auditOpinion,String id){
		if(StringUtils.isNotEmpty(entId)&&StringUtils.isNotEmpty(updateFlag)&&StringUtils.isNotEmpty(id)){
			//备案审核时保存企业历史信息
			EntHistory entHistory = new EntHistory();
			Ent ent=entDao.chenckEntInfo(entId);
			entHistory.setAreaCode(ent.getAreaCode());
			entHistory.setBankCard(ent.getBankCard());
			entHistory.setCardNo(ent.getCardNo());
			entHistory.setEntId(entId);
			entHistory.setAuditId(id);
			entHistory.setEntName(ent.getEntName());
			entHistory.setEntTypeCode(ent.getEntTypeCode());
			entHistory.setGradeType(ent.getGradeType());
			entHistory.setLegalIdCard(ent.getLegalIdCard());
			entHistory.setLegalPerson(ent.getLegalPerson());
			entHistory.setLegalPhone(ent.getLegalPhone());
			entHistory.setOpeningBank(ent.getOpeningBank());
			entHistory.setPayee(ent.getPayee());
			entHistory.setPreventNo(ent.getPreventNo());
			entHistory.setFarmType(ent.getEntDic().getDicInfoName());//养殖品种
			entHistory.setInitialDeposit(ent.getEntDic().getInitialDeposit());//存栏量
			entHistory.setYearDeposit(ent.getEntDic().getYearDeposit());//年出栏量
			entHistory.setWholeAddr(ent.getAddress().getWholeAddr()+ent.getAddress().getDetail());
			if (EnumProperty.AuditTypeEnum.SUCCESS.getKey().equals(updateFlag)){
				entHistory.setAuditStatus(EnumProperty.AuditTypeEnum.SUCCESS.getKey());
				entHistory.preInsert();
				entHistoryDao.insert(entHistory);
				auditDao.changeAuditStatus(id,entHistory.getId(),EnumProperty.AuditTypeEnum.SUCCESS.getKey(),auditOpinion,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),"bus_ent");
				entDao.updateEntStatus(entId,entHistory.getId(),EnumProperty.AuditTypeEnum.SUCCESS.getKey());
				return true;
			}
			if(EnumProperty.AuditTypeEnum.REJECT.getKey().equals(updateFlag)){
				entHistory.setAuditStatus(EnumProperty.AuditTypeEnum.REJECT.getKey());
				entHistory.preInsert();
				entHistoryDao.insert(entHistory);
				auditDao.changeAuditStatus(id,entHistory.getId(),EnumProperty.AuditTypeEnum.REJECT.getKey(),auditOpinion,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),"bus_ent");
				entDao.updateEntStatus(entId,entHistory.getId(),EnumProperty.AuditTypeEnum.REJECT.getKey());
				return true;
			}
		}
		return false;
	}
	/**
	 *
	 * @Title: findListByEntId
	 * @author: ZYJ
	 * @date: 2019年6月11日 下午3:58:50
	 * @Description: 获取我的已审批的通知
	 * @param: audit
	 * @return: List<Audit>
	 * @throws
	 */
	public Page<Audit> findListByEntId(Page<Audit> page) {
		Audit audit = new Audit();
		String userId = UserUtils.getUser().getId();
		audit.setUserId(userId);
		audit.setPage(page);
		List<Audit> list = auditDao.findListByEntId(audit);

		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		for (Audit audit2 : list) {
			//用于首页显示通知后面的时间
			long diff =  new Date().getTime() - audit2.getAuditDate().getTime();		// 获得两个时间的毫秒时间差异
			long day = diff / nd;		// 计算差多少天
			long hour = diff % nd / nh;		// 计算差多少小时
			long min = diff % nd % nh / nm;		// 计算差多少分钟
			if(day < 1){
				if(hour < 1){
					audit2.setRemarks(min + "分钟前");
				}else{
					audit2.setRemarks(hour + "小时前");
				}
			}else {
				String reamrks = new SimpleDateFormat("yyyy-MM-dd").format(audit2.getAuditDate()).toString();
				audit2.setRemarks(reamrks);
			}
			//用于通知页显示时间
			String auditDate = new SimpleDateFormat("yyyy-MM-dd").format(audit2.getAuditDate()).toString();//审核时间
			String now = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();//当前时间
			if(now.equals(auditDate)){
				audit2.setNoticeAuditDate(new SimpleDateFormat("HH:mm").format(audit2.getAuditDate()).toString());//审核时间是今天，显示时分
			}else{
				audit2.setNoticeAuditDate(auditDate);//不是今天，显示审核日期
			}
		}
		page.setList(list);
		return page;
	}


	/**
	 *
	 * @Title: updateNoticeStatus
	 * @author: ZYJ
	 * @date: 2019年6月12日 上午9:51:29
	 * @Description: 修改通知读取标识
	 * @return: void
	 * @throws
	 */
	@Transactional(readOnly = false)
	public void updateNoticeStatus() {
		String userId = UserUtils.getUser().getId();
		List<String> userIds = new ArrayList<>();
		userIds.add(userId);
		auditDao.updateNoticeStatus(userIds);
	}

	/**
	 * @Author zhangxingliang
	 * @Description 获取养殖场 审核状态
	 * @Date 18:49 2019/6/11
	 * @Param [tableId,tableName]
	 * @return java.lang.String
	 **/
	public Map<String, Object> getAuditStatus(@Param("tableId")String tableId, @Param("tableName")String tableName){return this.auditDao.getAuditStatus(tableId,tableName);};

	/**
	 *
	 * @Title: findNewestAuditByTblId
	 * @author: sy
	 * @date: 2019年06月13日 14:14
	 * @Description: 根据表ID获取最新的audit
	 * @param tblId
	 * @return: com.thinkgem.jeesite.modules.bus.entity.Audit
	 */
	public Audit findNewestAuditByTblId(String tblId,String tblName) {
		if(StringUtils.isBlank(tblId)||StringUtils.isBlank(tblName)){
			return new Audit();
		}
		return dao.findNewestAuditByTblId(tblId,tblName);
	}
	/**
	 * @author Zhang Xingliang
	 * @description 养殖场备案增加审核记录
	 * @date 20:16 2019/6/24
	 * @params [ent]
	 * @return void
	 **/
	@Transactional(readOnly = false)
	public void save(Ent ent) {
		Audit audit = new Audit();
		if("0".equals(ent.getAuditStatus()) && !"-1".equals(ent.getAuditId())){
			audit.setId(ent.getAuditId());
		}
		audit.setSubmitDate(new Date());
		audit.setUserId(ent.getId());
		audit.setTableId(ent.getId());
		audit.setTableName("bus_ent");
		audit.setAuditStatus(EnumProperty.AuditTypeEnum.UNREVIEWED.getKey());
		audit.setStatus(EnumProperty.DataStatus.ABLE.getKey());
		super.save(audit);
	}



}